﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.Role;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.SecurityConsole.Role
{
    public partial class RoleEdit : System.Web.UI.BasePage
    {
        private RoleService _roleServie;
        private RoleService RoleService
        {
            get
            {
                return _roleServie ??
                       (_roleServie = ObjectHelper.GetObject<RoleService>());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //角色类型和参与者类型不允许修改
            if (!IsPostBack && this.SmartFormView1.IsUpdate())
            {
                Control roleType = this.SmartFormView1.FindControl("Type");
                Control roleParticipant = this.SmartFormView1.FindControl("ParticipantType");
                if (roleType != null && roleParticipant != null)
                {
                    ((DropDownList)roleType).Enabled = false;
                    ((DropDownList)roleParticipant).Enabled = false;
                }
            }
        }

        protected void SaveOrUpdate(object sender, EventArgs e)
        {
            var roleInfo = this.SmartFormView1.ToEntity<RoleInfo>();

            this.RoleService.SaveOrUpdate(roleInfo);

            ScriptHelper.CloseDialog(this, "true");
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlType = sender as DropDownList;
            DropDownList ddlParticipantType = SmartFormView1.FindControl("ParticipantType") as DropDownList;
            Literal ltlParticipantType = SmartFormView1.FindControl("ltlParticipantType") as Literal;
            string typeValue = ddlType.SelectedValue;
            if (typeValue == "1" || typeValue == "2")
            {
                ltlParticipantType.Text = "参与者角色:<font color='red'>*<font>";
                ddlParticipantType.Enabled = true;
                ddlParticipantType.Attributes.Add("validator", "required");
            }
            else
            {
                ltlParticipantType.Text = "参与者角色:";
                ddlParticipantType.SelectedValue = "";
                ddlParticipantType.Enabled = false;
                ddlParticipantType.Attributes.Remove("validator");
            }
        }
    }
}